package cn.tx.erp.sys.controller;

import cn.tx.erp.sys.domain.Notice;
import cn.tx.erp.sys.domain.User;
import cn.tx.erp.sys.service.NoticeService;
import cn.tx.erp.sys.utils.DataGridView;
import cn.tx.erp.sys.utils.ResultObj;
import cn.tx.erp.sys.utils.WebUtils;
import cn.tx.erp.sys.vo.NoticeVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.junit.platform.commons.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @author Mxb
 * @version 1.0
 * @date 2020/2/6 17:02
 */
@RestController
@RequestMapping("notice")
public class NoticeController {
    @Autowired
    private NoticeService noticeService;

    /**
     * 加载所有公告
     * @param noticeVo
     * @return
     */
    @RequestMapping("loadAllNotice")
    public DataGridView loadAllNotice(NoticeVo noticeVo) {
        //使用分页
        IPage<Notice> page = new Page<Notice>(noticeVo.getPage(), noticeVo.getLimit());
        QueryWrapper queryWrapper = new QueryWrapper();
        //模糊查询
        queryWrapper.like(StringUtils.isNotBlank(noticeVo.getTitle()), "title", noticeVo.getTitle());
        queryWrapper.like(StringUtils.isNotBlank(noticeVo.getOpername()), "opername", noticeVo.getOpername());
        //
        queryWrapper.ge(noticeVo.getStarttime() != null, "createtime", noticeVo.getEndtime());
        queryWrapper.le(noticeVo.getEndtime() != null, "createtime", noticeVo.getEndtime());
        //按照哪一列 升序排列
        queryWrapper.orderByDesc("createtime");
        this.noticeService.page(page, queryWrapper);
        //返回记录和总记录
        return new DataGridView(0, "", page.getTotal(), page.getRecords());

    }

    /**
     * 添加
     * @param noticeVo
     * @return
     */
    @RequestMapping(value = "/addNotice")
    public ResultObj addNotice(NoticeVo noticeVo) {
        try {
            noticeVo.setCreatetime(new Date());
            User sys_user = (User) WebUtils.getSession().getAttribute("user");
            noticeVo.setOpername(sys_user.getName());
            this.noticeService.save(noticeVo);
            return ResultObj.ADD_SUCCESS;
        } catch (Exception e) {
            return ResultObj.ADD_ERR;
        }
    }

    /**
     * 更新
     * @param noticeVo
     * @return
     */
    @RequestMapping(value = "/updateNotice")
    public ResultObj updateNotice(NoticeVo noticeVo) {
        try {
            this.noticeService.updateById(noticeVo);
            return ResultObj.UPDATE_SUCCESS;
        } catch (Exception e) {
            return ResultObj.UPDATE_ERR;
        }
    }


    /**
     * 删除
     * @param noticeVo
     * @return
     */
    @RequestMapping(value = "/deleteNotice")
    public ResultObj deleteNotice(NoticeVo noticeVo) {
        try {
            this.noticeService.removeById(noticeVo.getId());
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            return ResultObj.DELETE_ERR;
        }
    }

}
